<?php

/**
 * Parse de Atoms Feeds
 *
 * @author agustin
 */
class Atom extends Feed {

  public function toArray($limit = 0){
    if(isset($this->xml_feed)){
      $result = array();
      $cnt = 0;
      foreach ($this->xml_feed->entry as $entry) {
        $reg['title'] = $entry->title;        
        $reg['link'] = $entry->link->attributes()->href;
        $result[] = $reg;
        if($limit > 0){
          $cnt++;
          if($cnt == $limit){
            break;
          }
        }
      }
      return $result;      
    }else{
      throw new Exception('No data');
    }
  }

  public function toList($limit = 0){
    if(isset($this->xml_feed)){
      $format = '<li class="element">
                    <a href="%s%s">
                      %s
                    </a>
                 </li>';      
      $cnt = 0;
      $result = $this->getStyleSheet();
      $result .= '<ul id="feeds">';
      foreach ($this->xml_feed->entry as $entry) {
        $result .= sprintf($format,$this->domain,
                                   $entry->link->attributes()->href,
                                   $entry->title);
        if($limit > 0){
          $cnt++;
          if($cnt == $limit){
            break;
          }
        }
      }
      $result .= '</ul>';
      return $result;
    }else{
      throw new Exception('No data');
    }
  }
}
?>
